package com.nd.android.musicboxlibrary.interf.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.business.comm.MagicNumber;
import com.nd.android.musicboxlibrary.interf.IPlayerManager;
import com.nd.android.musicboxlibrary.interf.ManagerFactory;
import com.nd.android.musicboxlibrary.service.player.MusicService;
import com.nd.android.musicboxlibrary.ui.player.PlayerActivity;
import com.nd.android.musicboxlibrary.util.ConfigHelper;
import com.nd.android.musicboxlibrary.util.MusicBoxUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 播放器控制 <br>
 * Created 2014年7月11日 下午10:56:17
 *
 * @version
 * @author wat.ztag
 *
 * @see
 */
public class PlayerManager implements IPlayerManager, MagicNumber {
//    private static final String MUSIC_SERVICE_ACTION = MusicService.class.getName();
    private MusicInfo mCurrentMusicInfo;
    private int mPlayerState;

    @Override
    public void seekTo(Context context, int position) {
        Intent intent = MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_SEEK_TO);
        intent.putExtra(MusicService.MUSIC_SEEKTO, position);
        context.startService(intent);
    }

    @Override
    public void initMusicList(Context context) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_LIST);
        context.startService(intent);
    }

    @Override
    public void stop(Context context) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_STOP);
        context.startService(intent);
    }

    @Override
    public void play(Context context) {
        play(context, 0);
    }

    @Override
    public void play(Context context, int position) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PLAY);
        if (position > 0) {
            intent.putExtra(MusicService.MUSIC_SEEKTO, position);
        }
        context.startService(intent);
    }

    @Override
    public void pause(Context context) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PAUSE);
        context.startService(intent);
    }

    @Override
    public void prev(Context context) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PREV);
        context.startService(intent);
    }

    @Override
    public void next(Context context) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_NEXT);
        context.startService(intent);
    }

    @Override
    public void updateLoopType(Context context, int loopType) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_LOOP_TYPE);
        intent.putExtra(MusicService.LOOP_TYPE, loopType);
        context.startService(intent);
        ConfigHelper.getInstance(context).saveIntKey(MusicService.LOOP_TYPE, loopType);
    }

    @Override
    public void play(Context context, MusicInfo musicInfo) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PLAY);
        intent.putExtra(MusicService.MUSIC_INFO, musicInfo);
        context.startService(intent);
    }

    @Override
    public void playByItem(Context context, MusicInfo musicInfo, List<MusicInfo> list, int index) {
        playByItem(context, musicInfo, list, index, true);
    }

    @Override
    public void playByItem(Context context, MusicInfo musicInfo, List<MusicInfo> list, int index, boolean needClosePage) {
        if (list != null) {
            ArrayList<MusicInfo> playList = new ArrayList<MusicInfo>();
            int size = list.size();
            if (size - index < n30) {
                if (size >= n30) {
                    playList.addAll(list.subList(size - n30, size));
                } else {
                    playList.addAll(list);
                }
            } else {
                for (int i = index; i < size; i++) {
                    if (i - index < n30) {
                        playList.add(list.get(i));
                    }
                }
            }
            ManagerFactory.getPlaylistInstance(context).replaceMusicListOfPlaying(playList);
            ManagerFactory.getPlayerInstance().initMusicList(context, playList);
        }
        ManagerFactory.getPlayerInstance().play(context, musicInfo);
        if (needClosePage) {
            PlayerActivity.removeSubActivity();
            if(context instanceof Activity){
            	((Activity) context).finish();        	
            }
        }
    }

    @Override
    public void playByRandom(Context context, List<MusicInfo> list) {
        Collections.shuffle(list);//随机打散列表数据
        ArrayList<MusicInfo> playList = new ArrayList<MusicInfo>();
        if (list.size() > n30) {
            playList.addAll(list.subList(0, n30));
        } else {
            playList.addAll(list);
        }
        ManagerFactory.getPlaylistInstance(context).replaceMusicListOfPlaying(playList);
        ManagerFactory.getPlayerInstance().initMusicList(context, playList);//初始化播放列表,并把列表发送给MusicService
        ManagerFactory.getPlayerInstance().updateLoopType(context, MusicService.LOOP_TYPE_RADOM);
        ManagerFactory.getPlayerInstance().play(context, list.get(0));
        PlayerActivity.removeSubActivity();
        if(context instanceof Activity){
        	((Activity) context).finish();        	
        }
    }

    @Override
    public void initMusicList(Context context, ArrayList<? extends MusicInfo> list) {
        Intent intent =  MusicBoxUtils.getMusicService(context);
        intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_LIST);
        intent.putExtra(MusicService.MUSIC_LIST, list);
        context.startService(intent);
    }

    @Override
    public void playBySpecialItem(Context context, MusicInfo musicInfo, ArrayList<MusicInfo> list) {
        if (list != null) {
            ManagerFactory.getPlaylistInstance(context).replaceMusicListOfPlaying(list);
            ManagerFactory.getPlayerInstance().initMusicList(context, list);
        }
        ManagerFactory.getPlayerInstance().play(context, musicInfo);
        PlayerActivity.removeSubActivity();
        if(context instanceof Activity){
        	((Activity) context).finish();        	
        }
    }

    @Override
    public MusicInfo getCurrentMusicInfo() {
        return mCurrentMusicInfo;
    }

    @Override
    public void setCurrentMusic(MusicInfo musicInfo) {
        this.mCurrentMusicInfo = musicInfo;
    }

    @Override
    public void setPlayerState(int state) {
        this.mPlayerState = state;
    }

    @Override
    public int getPlayerState() {
        return mPlayerState;
    }

    @Override
    public void playByOtherList(Context context, ArrayList<? extends MusicInfo> list,
                                MusicInfo music) {
        if (list != null && music != null) {
            ManagerFactory.getPlaylistInstance(context).replaceMusicListOfPlaying(list);
            ManagerFactory.getPlayerInstance().initMusicList(context, list);
            ManagerFactory.getPlayerInstance().play(context, music);
        }
    }

    @Override
    public void playByOtherList(Context context, ArrayList<? extends MusicInfo> list, int index) {
        if (list != null && list.size() > index) {
            playByOtherList(context, list, list.get(index));
        }
    }

    /**
     *
     * <br>Created 2014年11月28日 上午12:05:46
     * @author       wat.ztag
     */
    @Override
    public void initMagicNumber() {
    }
}
